﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtNetwork>
#include <QFileDialog>
#pragma execution_character_set("utf-8")

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("ChatInNet");

    m_setupwindow = new SetUpWindow();

    m_tcpfilePOST = new tcpFilePost();
    m_tcpfileGET = new tcpFileGet();
    m_udpmesPOST = new udpMesPost();
    m_udpmesGET = new udpMesGet();

    m_textbrowCON = new textBrowserCon(ui->tb_info);

    connect(m_tcpfilePOST,SIGNAL(sig_PostProgress(int)),this,SLOT(sendFileProgress(int)));
    connect(m_tcpfilePOST,SIGNAL(sig_PostError(QString)),this,SLOT(sendFileError(QString)));
    connect(m_tcpfileGET,SIGNAL(sig_GetProgress(int)),this,SLOT(getFileProgress(int)));
    connect(m_tcpfileGET,SIGNAL(sig_GetError(QString)),this,SLOT(getFileError(QString)));
    connect(m_udpmesGET,SIGNAL(sig_GetProgress(QString)),this,SLOT(getMesProgress(QString)));
    connect(m_setupwindow,SIGNAL(sig_SetupWinClose(QString)),this,SLOT(setupWinClose(QString)));

    m_tcpfileGET->openlisten();
    m_udpmesGET->openlisten();

    // testing

    // m_userdataCON.open();
//    m_userdataCON.insert();
//    m_userdataCON.findall();
//    m_userdataCON.change();
//    m_userdataCON.findall();
//    // m_userdataCON.clear();
//    m_userdataCON.close();

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::closeEvent(QCloseEvent *event)
{
     qDebug() << "close MainWindow" << event;

     m_tcpfileGET->closelisten();
     m_udpmesGET->closelisten();
}

void MainWindow::setupWinClose(QString closemessage)
{
    m_tcpfileGET->closelisten();
    m_udpmesGET->closelisten();
    m_tcpfileGET->m_listenPort = closemessage;
    m_udpmesGET->m_listenPort = closemessage;
    m_tcpfileGET->openlisten();
    m_udpmesGET->openlisten();
}

void MainWindow::sendFileProgress(int progress)
{  
    QString info = QString("文件 %1 传送进度: %2 %").arg(m_sendfilename).arg(progress);
    m_textbrowCON->writeOverLine(info,Qt::AlignRight);

    if(progress==100){
        ui->pb_sendfile->setEnabled(true);
        m_textbrowCON->writeTimeStamp();
        m_textbrowCON->writeAppend("\n文件传送成功\n",Qt::AlignRight);
    }

}

void MainWindow::sendFileError(QString errormessage)
{
    QString info = QString("网络存在故障，请检查网络地址、端口等信息\n");
    m_textbrowCON->writeAppend(info,Qt::AlignCenter);
    m_textbrowCON->writeAppend(errormessage+"\n",Qt::AlignCenter);

    ui->pb_sendfile->setEnabled(true);

}

void MainWindow::getFileProgress(int progress)
{
    QString info = QString("文件 %1 传送进度: %2 %").arg(m_sendfilename).arg(progress);
    m_textbrowCON->writeOverLine(info,Qt::AlignLeft);

    if(progress==100){
        ui->pb_sendfile->setEnabled(true);
        m_textbrowCON->writeTimeStamp();
        m_textbrowCON->writeAppend("\n文件传送成功\n",Qt::AlignLeft);
    }

}

void MainWindow::getFileError(QString errormessage)
{
    QString info = QString("网络存在故障，请检查网络地址、端口等信息\n");
    m_textbrowCON->writeAppend(info,Qt::AlignCenter);
    m_textbrowCON->writeAppend(errormessage+"\n",Qt::AlignCenter);

}

void MainWindow::getMesProgress(QString message)
{
    m_textbrowCON->writeTimeStamp();
    m_textbrowCON->writeAppend(message,Qt::AlignLeft);

}

void MainWindow::on_pb_sendfile_clicked()
{
    m_sendfilename=QFileDialog::getOpenFileName(this);
    if(!m_sendfilename.isEmpty()){
        m_textbrowCON->writeTimeStamp();
        m_textbrowCON->writeAppend("TCP连接中......\n",Qt::AlignRight);

        ui->pb_sendfile->setEnabled(false);
        m_tcpfilePOST->send(ui->hostLineEdit->text(),ui->portLineEdit->text(),m_sendfilename);
    }
    else{
        ui->pb_sendfile->setEnabled(true);
    }
}

void MainWindow::on_pb_send_clicked()
{
    QString sendmessage = ui->te_input->toPlainText();

    m_udpmesPOST->send(ui->hostLineEdit->text(),ui->portLineEdit->text(),sendmessage);
    m_textbrowCON->writeTimeStamp();
    m_textbrowCON->writeAppend(sendmessage,Qt::AlignRight);

    ui->te_input->clear();

}

void MainWindow::on_pb_setup_clicked()
{
    m_setupwindow->setParent(this);
    m_setupwindow->setWindowFlags(m_setupwindow->windowFlags()|Qt::Dialog);
    m_setupwindow->show();
}
